package com.example.nyf.common.shiro;

import com.example.nyf.entity.SysUser;
import com.example.nyf.service.SysRoleService;
import com.example.nyf.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysRoleService sysRoleService;

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("-------doGetAuthorizationInfo 授权--------");
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) principalCollection.getPrimaryPrincipal();

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        if(sysUser.getUser_name() == "admin") {
            simpleAuthorizationInfo.addRole("admin");
            simpleAuthorizationInfo.addStringPermission("*:*:*");
        } else {
            Set<String> roles = new HashSet<String>();
            Set<String> menus = new HashSet<String>();

            roles = sysRoleService.getSysRoleByUserId(sysUser.getId());
            menus = sysRoleService.getSysPerByUserId(sysUser.getId());
            simpleAuthorizationInfo.addRoles(roles);
            simpleAuthorizationInfo.addStringPermissions(menus);

        }

        sysUser.setLogin_time((int)(System.currentTimeMillis()/1000));

        sysUserService.updateSysUserLoginInfo(sysUser);

        return simpleAuthorizationInfo;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;

        String userName = upToken.getUsername();
        String password = "";
        if (upToken.getPassword() != null) {
            password = new String(upToken.getPassword());
        }
        if(userName.trim().equals("") || password.trim().equals("")) {
            throw new UnknownAccountException("用户名或密码不能为空");
        }
        // 获取用户信息
        SysUser sysUser = sysUserService.getSysUserByName(userName);
        if(sysUser == null) {
            throw new UnknownAccountException("用户名不存在");
        }
        if(sysUser.getStatus() == 1) {
            throw new AuthenticationException("账号已停用");
        }
        String passWord = sysUser.getPassword();
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(sysUser, passWord, this.getName());
        System.out.println("-------doGetAuthenticationInfo 验证--------");
        return info;
    }

}
